//
//  HeroBaseHandle.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/29.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "HeroBaseHandle.h"

@implementation HeroBaseHandle



+(instancetype)ShareHeroHeroDate
{

    static HeroBaseHandle *hero = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        hero = [[HeroBaseHandle alloc] init];
        
    });
    
    return hero;
}



static sqlite3 *db = nil;



-(void)openDB
{

    if (db != nil) {
        
        return;
    }
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"deateBase.sqlite"];
    
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    
    if (result == SQLITE_OK) {
        
    }else
    {
    
    }
    
    
    
}

-(void)createHeroTable
{

NSString *sql = @"CREATE TABLE IF NOT EXISTS hero (number INTEGER PRIMARY KEY AUTOINCREMENT, cnName TEXT, enName TEXT, location TEXT, price TEXT, rating TEXT, tags TEXT, title TEXT)";

    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
    }else
    {
    
    
    }
    
    

}



-(void)insertHero:(HeroModel *)hero
{

    NSString *sql = [NSString stringWithFormat:@"INSERT INTO hero (cnName, enName, location, price, rating, tags, title) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@')",hero.cnName, hero.enName, hero.location, hero.price, hero.rating, hero.tags, hero.title];
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
    }else
    {
    

    }
    
 
}


-(NSMutableArray *)selectAll
{

NSString *sql = @"SELECT *FROM hero";
    
    sqlite3_stmt *stmt = nil;

    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
//        NSLog(@"准备查询成功");
        
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *cnName = sqlite3_column_text(stmt, 1);
            const unsigned char *enName = sqlite3_column_text(stmt, 2);
            const unsigned char *location = sqlite3_column_text(stmt, 3);
            const unsigned char *price = sqlite3_column_text(stmt, 4);
            const unsigned char *rating = sqlite3_column_text(stmt, 5);
            const unsigned char *tags = sqlite3_column_text(stmt, 6);
            const unsigned char *title = sqlite3_column_text(stmt, 7);

            HeroModel *hero = [[HeroModel alloc] init];
            
            hero.cnName = [NSString stringWithUTF8String:(const char *)cnName];
            hero.enName = [NSString stringWithUTF8String:(const char *)enName];
            hero.location = [NSString stringWithUTF8String:(const char *)location];
            hero.price = [NSString stringWithUTF8String:(const char *)price];
            hero.rating = [NSString stringWithUTF8String:(const char *)rating];
            hero.tags = [NSString stringWithUTF8String:(const char *)tags];
            hero.title = [NSString stringWithUTF8String:(const char *)title];

            [array addObject:hero];
            
            [hero release];
        }
        
        sqlite3_finalize(stmt);
        
        return array ;
        
        
    }else
    {
//        NSLog(@"准备失败");
        return nil;
    }

}


-(void)dropHerotable
{
NSString *sql = @"DROP TABLE hero";
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);

    if (resuil == SQLITE_OK) {
        
//        NSLog(@"删除成功");
        
    }else
    {
    
//        NSLog(@"删除失败");
    }
    

}


-(void)createAllHeroTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS allHero (number INTEGER PRIMARY KEY AUTOINCREMENT, cnName TEXT, enName TEXT, location TEXT, price TEXT, rating TEXT, tags TEXT, title TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
//        NSLog(@"创建表成功");
    }else
    {
        
//        NSLog(@"创建表失败");
        
    }
    
    
    
}



-(void)insertAllHero:(HeroModel *)hero
{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO allHero (cnName, enName, location, price, rating, tags, title) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@')",hero.cnName, hero.enName, hero.location, hero.price, hero.rating, hero.tags, hero.title];
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
        
    }else
    {
        
//        NSLog(@"插入失败");
    }
    
    
}


-(NSMutableArray *)selectAllHero
{
    
    NSString *sql = @"SELECT *FROM allHero";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
//        NSLog(@"准备查询成功");
        
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *cnName = sqlite3_column_text(stmt, 1);
            const unsigned char *enName = sqlite3_column_text(stmt, 2);
            const unsigned char *location = sqlite3_column_text(stmt, 3);
            const unsigned char *price = sqlite3_column_text(stmt, 4);
            const unsigned char *rating = sqlite3_column_text(stmt, 5);
            const unsigned char *tags = sqlite3_column_text(stmt, 6);
            const unsigned char *title = sqlite3_column_text(stmt, 7);
            
            HeroModel *hero = [[HeroModel alloc] init];
            
            hero.cnName = [NSString stringWithUTF8String:(const char *)cnName];
            hero.enName = [NSString stringWithUTF8String:(const char *)enName];
            hero.location = [NSString stringWithUTF8String:(const char *)location];
            hero.price = [NSString stringWithUTF8String:(const char *)price];
            hero.rating = [NSString stringWithUTF8String:(const char *)rating];
            hero.tags = [NSString stringWithUTF8String:(const char *)tags];
            hero.title = [NSString stringWithUTF8String:(const char *)title];
            
            [array addObject:hero];
            
            [hero release];
        }
        
        sqlite3_finalize(stmt);
        
        return array ;
        
        
    }else
    {
//        NSLog(@"准备失败");
        return nil;
    }
    
}


-(void)dropAllHerotable
{
    NSString *sql = @"DROP TABLE allHero";
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (resuil == SQLITE_OK) {
        
//        NSLog(@"删除成功");
        
    }else
    {
        
//        NSLog(@"删除失败");
    }
    
    
}



//**************************************************************************      视频导航界面

// 创建视频导航界面表

-(void)createmyVideoTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS myVideo (number INTEGER PRIMARY KEY AUTOINCREMENT, icon TEXT, name TEXT, tag TEXT, myname TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
//                NSLog(@"创建表成功");
    }else
    {
        
//                NSLog(@"创建表失败");
        
    }
    
    
    
}

//  查询视频导航表

-(NSMutableArray *)selectAllmyVideo
{
    
    NSString *sql = @"SELECT *FROM myVideo";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
//                NSLog(@"准备查询成功");
        
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *icon = sqlite3_column_text(stmt, 1);
            const unsigned char *name = sqlite3_column_text(stmt, 2);
            const unsigned char *tag = sqlite3_column_text(stmt, 3);
            const unsigned char *myname = sqlite3_column_text(stmt, 4);
            
            VideoModel *myV = [[VideoModel alloc] init];
            
            myV.icon = [NSString stringWithUTF8String:(const char *)icon];
            myV.name = [NSString stringWithUTF8String:(const char *)name];
            myV.tag = [NSString stringWithUTF8String:(const char *)tag];
            myV.myname = [NSString stringWithUTF8String:(const char *)myname];
        
            [array addObject:myV];
            
            [myV release];
        }
        
        sqlite3_finalize(stmt);
        
        return array ;
        
    }else
    {
        //        NSLog(@"准备失败");
        return nil;
    }

        
}
        
        
        


//  插入视频导航

-(void)insertmyVideo:(VideoModel *)video myName:(NSString *)myname
{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO myVideo (icon, name, tag, myname) VALUES('%@', '%@', '%@', '%@')", video.icon, video.name, video.tag, myname];
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
//                NSLog(@"插入成功");
        
    }else
    {
        
//                NSLog(@"插入失败");
    }
    
    
}


// 删除视频导航界面表

-(void)dropmyVideoTable
{
    NSString *sql = @"DROP TABLE myVideo";
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (resuil == SQLITE_OK) {
        
//                NSLog(@"删除成功");
        
    }else
    {
        
//                NSLog(@"删除失败");
    }
    
    
}





//   ****************************  视频列表


// 创建视频列表界面表

-(void)createmyVideoListTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS myVideoList (number INTEGER PRIMARY KEY AUTOINCREMENT, vid TEXT, udb TEXT, letv_video_id TEXT, cover_url TEXT, title TEXT, channelId TEXT, video_length TEXT, letv_video_unique TEXT, upload_time TEXT, totalPage TEXT, tag TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
//                        NSLog(@"创建表成功");
    }else
    {
        
//                        NSLog(@"创建表失败");
        
    }
    


}


//  查询视频列表的表

-(NSMutableArray *)selectAllmyVideoList
{
    
    NSString *sql = @"SELECT *FROM myVideoList";
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
//                        NSLog(@"准备查询成功");
        
        NSMutableArray *array = [NSMutableArray array];
        
    while (sqlite3_step(stmt) == SQLITE_ROW) {
    const unsigned char *vid = sqlite3_column_text(stmt, 1);
    const unsigned char *udb = sqlite3_column_text(stmt, 2);
    const unsigned char *letv_video_id = sqlite3_column_text(stmt, 3);
    const unsigned char *cover_url = sqlite3_column_text(stmt, 4);
    const unsigned char *title = sqlite3_column_text(stmt, 5);
    const unsigned char *channelId = sqlite3_column_text(stmt, 6);
    const unsigned char *video_length = sqlite3_column_text(stmt, 7);
    const unsigned char *letv_video_unique = sqlite3_column_text(stmt, 8);
    const unsigned char *upload_time = sqlite3_column_text(stmt, 9);
    const unsigned char *totalPage = sqlite3_column_text(stmt, 10);
    const unsigned char *tag = sqlite3_column_text(stmt, 11);
    
    VideoListModel *myV = [[VideoListModel alloc] init];
    
    myV.vid = [NSString stringWithUTF8String:(const char *)vid];
    myV.udb = [NSString stringWithUTF8String:(const char *)udb];
    myV.letv_video_id = [NSString stringWithUTF8String:(const char *)letv_video_id];
    myV.cover_url = [NSString stringWithUTF8String:(const char *)cover_url];
    myV.title = [NSString stringWithUTF8String:(const char *)title];
    myV.channelId = [NSString stringWithUTF8String:(const char *)channelId];
    myV.video_length = [NSString stringWithUTF8String:(const char *)video_length];
    myV.letv_video_unique = [NSString stringWithUTF8String:(const char *)letv_video_unique];
    myV.upload_time = [NSString stringWithUTF8String:(const char *)upload_time];
    myV.totalPage = [NSString stringWithUTF8String:(const char *)totalPage];
    myV.tag = [NSString stringWithUTF8String:(const char *)tag];
    
    
    [array addObject:myV];
    
    [myV release];
}

        sqlite3_finalize(stmt);
        
        return array ;
        
    }else
    {
//                NSLog(@"准备失败");
        return nil;
    }
    
    
}


//  插入视频列表

-(void)insertmyVideoList:(VideoListModel *)video

{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO myVideoList (vid, udb, letv_video_id, cover_url, title, channelId, video_length, letv_video_unique, upload_time, totalPage, tag) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", video.vid, video.udb, video.letv_video_id, video.cover_url, video.title, video.channelId, video.video_length, video.letv_video_unique, video.upload_time, video.totalPage, video.tag];
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
//                        NSLog(@"插入成功");
        
    }else
    {
        
//                        NSLog(@"插入失败");
    }
    
    
}



// 删除视频列表表
-(void)dropmyVideoListTable:(NSString *)mytag
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM myVideoList WHERE tag = '%@'", mytag];
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (resuil == SQLITE_OK) {
        
//                                NSLog(@"删除成功");
        
    }else
    {
        
//                                NSLog(@"删除失败");
    }
    
    
}






//   ******************************         装备



// 创建表




-(void)createEquipmentTable
{
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS equipment (number INTEGER PRIMARY KEY AUTOINCREMENT, equipmentID TEXT, equipmentText TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        
        //        NSLog(@"创建装备表");
        
    }else
    {
        
        //                NSLog(@"创建表失败");
        
    }
    
    
}



//  插入装备


-(void)insertEquipment:(EquipmentModel *)equipment
{

    NSString *sql = [NSString stringWithFormat:@"INSERT INTO equipment (equipmentID, equipmentText) VALUES('%@', '%@')",equipment.equipentId, equipment.text];

    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        //                NSLog(@"插入成功");
        
    }else
    {
        
        //                NSLog(@"插入失败");
    }
    
    
}


-(NSMutableArray *)selectEquipment
{

    NSString *sql = @"SELECT *FROM equipment";
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);

    if (result == SQLITE_OK) {
        
//        NSLog(@"准备查询装备成功");

        
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
            const unsigned char *text = sqlite3_column_text(stmt, 2);
            
            EquipmentModel *equipment = [[EquipmentModel alloc] init];
            
            equipment.equipentId = [NSString stringWithUTF8String:(const char *)ID];
            equipment.text = [NSString stringWithUTF8String:(const char *)text];
            
            [array addObject:equipment];
            
            [equipment release];
            
            
        }
        
        
        sqlite3_finalize(stmt);
        
        return array;
        

    }else
    {
        //        NSLog(@"准备失败");
        return nil;
    }
    
    
}




-(void)dropEquipment
{
NSString *sql = @"DROP TABLE equipment";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);

    if (result == SQLITE_OK) {
        
//        NSLog(@"删除装备表成功");
        
    }else
    {
    
//        NSLog(@"删除装备表失败");
    }


}



-(void)createNewsTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS news (number INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT, desc TEXT, posttime TEXT,thumb_img TEXT, newid TEXT)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result  == SQLITE_OK) {
//        NSLog(@"创建表格成功");
    }else{
        
//        NSLog(@"创建表格失败");
        
    }
    
    
    
    
}

-(void)insertNews:(NewsModel *)news
{
    NSString * sql = [NSString stringWithFormat:@"INSERT INTO news(title, desc, posttime, thumb_img,newid)VALUES('%@','%@','%@','%@','%@')",news.title,news.desc, news.posttime, news.thumb_img, news.newid];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }else{
        
//        NSLog(@"插入失败");
    }
   
    
}

-(NSMutableArray *)selectNews
{
     NSString *sql = @"SELECT *FROM news";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
//        NSLog(@"准备查询成功");
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *desc = sqlite3_column_text(stmt, 2);
            const unsigned char *posttime = sqlite3_column_text(stmt, 3);
            const unsigned char *thumb_img = sqlite3_column_text(stmt, 4);
            const unsigned char *newid = sqlite3_column_text(stmt, 5);
            NewsModel *new = [[NewsModel alloc] init];
            
            new.title = [NSString stringWithUTF8String:(const char *)title];
             new.desc = [NSString stringWithUTF8String:(const char *)desc];
             new.posttime = [NSString stringWithUTF8String:(const char *)posttime];
             new.thumb_img = [NSString stringWithUTF8String:(const char *)thumb_img];
             new.newid = [NSString stringWithUTF8String:(const char *)newid];
            [array addObject:new];
            
            [new release];
        }
        
        sqlite3_finalize(stmt);
        return array;
        
    }else{
//        NSLog(@"准备失败");
        return nil;
        
        
    }
    
    
    
    
    
    
    
}




-(void)dropNewsTable
{
    NSString *sql = @"DROP TABLE news";
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (resuil == SQLITE_OK) {
        
//              NSLog(@"删除成功");
        
    }else
    {
        
//            NSLog(@"删除失败");
    }
    
    
}

//-----------------
-(void)createScrollTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS scroll(number INTEGER PRIMARY KEY AUTOINCREMENT,img TEXT, myId TEXT)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (result  == SQLITE_OK) {
//        NSLog(@"创建表格成功");
    }else{
        
//        NSLog(@"创建表格失败");
        
    }
    
    
    
    
}

-(void)insertScroll:(ScrollModel *)scroll
{
    NSString * sql = [NSString stringWithFormat:@"INSERT INTO scroll(img, myId)VALUES('%@','%@')",scroll.img, scroll.myId];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, nil);
    
    
    if (result == SQLITE_OK) {
//        NSLog(@"插入成功");
    }else{
        
//        NSLog(@"插入失败");
    }
    
    
}

-(NSMutableArray *)selectScroll
{
    NSString *sql = @"SELECT *FROM scroll";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
//        NSLog(@"准备查询成功");
        NSMutableArray *array = [NSMutableArray array];
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *img = sqlite3_column_text(stmt, 1);
            const unsigned char *myId = sqlite3_column_text(stmt, 2);
            
            ScrollModel *scro = [[ScrollModel alloc] init];
            
            scro.img = [NSString stringWithUTF8String:(const char *)img];
            scro.myId = [NSString stringWithUTF8String:(const char *)myId];
           
            
            [array addObject:scro];
            
            [scro release];
        }
        
        sqlite3_finalize(stmt);
        return array;
        
    }else{
//        NSLog(@"准备失败");
        return nil;
        
        
    }
    
    
    
    
    
    
    
}




-(void)dropScrollTable
{
    NSString *sql = @"DROP TABLE scroll";
    
    int resuil = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    
    if (resuil == SQLITE_OK) {
        
//        NSLog(@"删除成功");
        
    }else
    {
        
//        NSLog(@"删除失败");
    }
    
    
}













@end
